package club.adger.${module}.controller.admin;

import club.adger.server.dto.${Entity}Dto;
import club.adger.server.dto.PageDto;
import club.adger.server.dto.ResponseDto;
import club.adger.server.service.${Entity}Service;
import club.adger.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Adger
 * @Date: 2020/07/25/5:28
 * 前端会受到两个参数 自己去设置两个参数 默认查询全部
 */
@RestController
@RequestMapping("/admin/${entity}")
public class ${Entity}Controller {
    private static final Logger LOG = LoggerFactory.getLogger(${Entity}Controller.class);
    public static final String BUSINESS_NAME = "${tableNameCn}";

    @Resource
    private ${Entity}Service ${entity}Service;

    /**
     * 列表查询
     * @param pageDto
     * @return
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        ${entity}Service.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 增加或者修改 根据 id 有没有值来判断的
     * @param ${entity}Dto
     * @return
     */
    @PostMapping("/add")
    public ResponseDto add(@RequestBody ${Entity}Dto ${entity}Dto) {
// 保存校验
<#list fieldList as field>
    <#if field.name!="id" && field.nameHump!="createdAt" && field.nameHump!="updatedAt" && field.nameHump!="sort">
        <#if !field.nullAble>
            ValidatorUtil.require(${entity}Dto.get${field.nameBigHump}(), "${field.nameCn}");
        </#if>
        <#if (field.length > 0)>
            ValidatorUtil.length(${entity}Dto.get${field.nameBigHump}(), "${field.nameCn}", 1, ${field.length?c});
        </#if>
    </#if>
</#list>
        ResponseDto responseDto = new ResponseDto();
        ${entity}Service.add(${entity}Dto);
        responseDto.setContent(${entity}Dto);
        return responseDto;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        ${entity}Service.delete(id);
        return responseDto;
    }
}
